import Sheet from '../component/sheet';
import { h } from '../component/element';
import { cssPrefix } from '../config';

export default class MyWebSocket{
  constructor(rootEl, data, targetEl, spreadsheet){
    const _this = this;
    const ws = window.ws;

    ws.onmessage = function(e){
      //当客户端收到服务端发来的消息时，触发onmessage事件，参数e.data包含server传递过来的数据
      //接收数据注意处理type
      let res = JSON.parse(e.data)
      console.log('onmessage',res); 

      let muAreaArr = res.sheet;
      rootEl = h('div', `${cssPrefix}`).on('contextmenu', evt => evt.preventDefault());
      window.sheet = new Sheet(rootEl, data, muAreaArr);
      // targetEl.children[0].children[1].children[1].innerHTML = rootEl.el.children[1].children[1].innerHTML
      // targetEl.replaceChildren(rootEl.el);
      // rootEl.child(spreadsheet.bottombar.el); //添加底部

      spreadsheet.loadData(res.data).change(() => {
        console.log('>>>', xs.getData());
      })
    }

    ws.onclose = function(e){
      //当客户端收到服务端发送的关闭连接请求时，触发onclose事件
      console.log("close");
    }

    ws.onerror = function(e){
      //如果出现连接、处理、接收、发送数据失败的时候触发onerror事件
      console.log(error);
    }
  }
}